extends Control



var Item = preload("res://tool/page/recoverCenter/recoverCenterItem.tscn")

onready var search_input = $searchBox/LineEdit 

# 当前正在编辑的剧本数据，添加剧本数据为空
var edit_drama_data = null


func _ready():
	state_update()


# 刷新页面状态
func state_update():
	update_list(DataSource.get_data_list(DataSource.map_center_recover))

func update_list(list):
	
	var box = $table/ScrollContainer/VBoxContainer
	
	for child in box.get_children():
		child.queue_free()
		
	for npc_data in DataSource.get_data_list(DataSource.map_center_recover):
		var ui_item = Item.instance()
		ui_item.set_data(npc_data)
		box.add_child(ui_item)
		
		ui_item.connect("go_map", self, "_on_go_map")


func _on_go_map(map_data, npc_data):
	var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/MapView.tscn", 0, false)
	ui.set_data(map_data)
	
	ui.connect("close", self, "_on_map_close")
	
	ui.select_grid(npc_data["x"], npc_data["y"])
	

func _on_map_close():
	state_update()


func _on_search_button_up():
	var text = search_input.text
	
	if text:
		var list = []
		for map in DataSource.get_data_list(DataSource.map_center_recover):
			if map.get("name", "").find(text) != -1:
				list.append(map)
				
		update_list(list)
	else:
		update_list(DataSource.get_data_list(DataSource.map_center_recover))
